import { getParents } from '@/common/dom'
export default {
  bind(el, binding, vnode) {
    function documentHandler (e) {
      if (el.contains(e.target)) {
        return false
      }
      //配合transfer-dom 命令 对于在移出的元素中的点击事件，找到其绑定元素，判断其是否在click-out绑定元素中，来决定是否触发click-out事件
      let transferParent = getParents(e.target, (item) => {
        return item.__transferDomData ? true : false
      })
      if (transferParent && el.contains(transferParent.__transferDomData.parentNode)) {
        return false
      }
      if (binding.expression) {
        binding.value(e)
      }
    }
    el.__vueClickOutside__ = documentHandler
    document.addEventListener('click', documentHandler)
  },
  update() {},
  unbind(el, binding) {
    document.removeEventListener('click', el.__vueClickOutside__)
    delete el.__vueClickOutside__
  }
}
